<template>
  <div class="tags-container">
    <el-card>
      <!-- 面包屑 -->
      <template
        v-if="subject.id&&subject.name"
        #header
      >
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ subject.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
      </template>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="18">
          <el-form
            label-width="80px"
            size="small"
            :inline="true"
          >
            <el-form-item label="标签名称">
              <el-input
                v-model="requestParams.tagName"
                style="200px"
              />
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="requestParams.state">
                <el-option
                  :value="1"
                  label="启用"
                />
                <el-option
                  :value="0"
                  label="禁用"
                />
              </el-select>
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="clear()">清除</el-button>
              <el-button
                type="primary"
                @click="filter()"
              >搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col
          :span="6"
          style="text-align:right"
        >
          <el-button
            v-if="subject.id && subject.name"
            type="text"
            icon="el-icon-back"
            @click="$router.back()"
          >返回学科</el-button>
          <el-button
            icon="el-icon-edit"
            type="success"
            size="small"
            @click="openDialog()"
          >新增标签</el-button>
        </el-col>
      </el-row>
      <!-- 数据记录 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom:15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      />
      <!-- 表格 -->
      <el-table :data="tags">
        <el-table-column
          label="序号"
          type="index"
          width="80px"
        />
        <el-table-column
          label="所属学科"
          prop="subjectName"
        />
        <el-table-column
          label="标签名称"
          prop="tagName"
        />
        <el-table-column
          label="创建者"
          prop="username"
        />
        <el-table-column label="创建日期">
          <template slot-scope="scope">{{ scope.row.addDate|parseTimeByString }}</template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="{row}">{{ row.state===1?'已启用':'已禁用' }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="150px"
        >
          <template #default="{row}">
            <el-button
              type="text"
              @click="toggleState(row)"
            >{{ row.state===1?'禁用':'启用' }}</el-button>
            <el-button
              type="text"
              :disabled="row.state===1"
              @click="openDialog(row)"
            >修改</el-button>
            <el-button
              type="text"
              :disabled="row.state===1||row.totals > 0"
              @click="delTag(row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px;text-align:right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        :page-sizes="[10,20,30,40,50]"
        hide-on-single-page
        @current-change="pager"
        @size-change="handleSizeChange"
      />
    </el-card>
    <!-- 添加|修改 -->
    <tags-add
      ref="tagssAdd"
      :data="currTag"
      @updateList="updateList()"
    />
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags'
import TagsAdd from '../components/tags-add'
export default {
  name: 'TagsList',
  components: { TagsAdd },
  data() {
    return {
      requestParams: {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      total: 0,
      tags: [],
      currTag: {}
    }
  },
  computed: {
    subject() {
      return this.$route.query || {}
    }
  },
  watch: {
    '$route.query': function() {
      this.getList()
    }
  },
  created() {
    this.getList()
  },
  methods: {
    openDialog(tag = {}) {
      this.currTag = tag
      this.$nextTick(() => {
        this.$refs.tagssAdd.open()
      })
    },
    // 切换状态
    async toggleState(tag) {
      await changeState({
        id: tag.id,
        state: tag.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      tag.state = tag.state === 1 ? 0 : 1
    },
    async delTag(tag) {
      await this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(tag)
      this.$message.success('删除成功')
      this.getList()
    },
    // 清除筛选条件
    clear() {
      this.requestParams = {
        tagsName: null,
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    // 进行筛选
    filter() {
      this.requestParams.page = 1
      this.getList()
    },
    // 查询列表
    async getList() {
      this.requestParams.subjectID = this.subject.id || null
      const { data } = await list(this.requestParams)
      this.tags = data.items
      this.total = data.counts
    },
    // 切换条数
    handleSizeChange(size) {
      this.requestParams.page = 1
      this.requestParams.pagesize = size
      this.getList()
    },
    // 切换分页
    pager(np) {
      this.requestParams.page = np
      this.getList()
    },
    // 新增|修改 后更新列表
    updateList() {
      if (!this.currTag.id) {
        this.requestParams.page = 1
      }
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.tags-container {
  padding: 10px;
}
</style>
